#!/bin/sh
################################################################################
# conf_sequential
#
# This script retrieves and processes the MiniMyth configure file.
################################################################################
. /etc/rc.d/functions

conf_variable_write() {
    /bin/echo -n ${2} >> ${1}
    /bin/echo -n "="  >> ${1}
    /bin/echo -n "'"  >> ${1}
    /bin/echo -n ${3} >> ${1}
    /bin/echo    "'"  >> ${1}

    return 0
}

start() {

    local file
    local file_list
    local variable

    /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/conf_sequential] Starting..."

    # Unset variables set by the processed MiniMyth configuration file
    # This is a hack for testing that should never get invoked during normal boot
    if [ -e /etc/conf.d/dhcp.override ] ; then
        /bin/cat /etc/conf.d/dhcp.override | /bin/grep -e '^MM_[^=]*=' | /bin/sed -e 's%=.*$%%' | \
        while read variable ; do
            set ${variable}=''
        done
        /bin/rm -f '/etc/conf.d/dhcp.override'
    fi
    if [ -e /etc/conf.d/minimyth ] ; then
        /bin/cat /etc/conf.d/minimyth | /bin/grep -e '^MM_[^=]*=' | /bin/sed -e 's%=.*$%%' | \
        while read variable ; do
            set ${variable}=''
        done
        /bin/rm -f '/etc/conf.d/minimyth'
    fi

    # Read core and dhcp configuration files
    /usr/bin/test -e /etc/conf.d/core && . /etc/conf.d/core
    /usr/bin/test -e /etc/conf.d/dhcp && . /etc/conf.d/dhcp

    mm_message_output info "downloading configuration ..."

    # Determine current boot directory location
    MM_MINIMYTH_BOOT_URL=${MM_MINIMYTH_BOOT_URL:-${MM_TFTP_BOOT_URL}}
    MM_MINIMYTH_BOOT_URL=${MM_MINIMYTH_BOOT_URL:-'file:/minimyth/'}

    # Store DHCP & TFTP server info
    MM_MINIMYTH_DHCP_SERVER=${MM_MINIMYTH_DHCP_SERVER:-${MM_DHCP_SERVER}}
    MM_MINIMYTH_TFTP_SERVER=${MM_MINIMYTH_TFTP_SERVER:-${MM_TFTP_SERVER}}

    # Get MiniMyth configuration file
    /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/conf_sequential] Trying to download minimyth.conf..."
    mm_confro_get /minimyth.conf /etc/minimyth.d/minimyth.conf.remote

    # Make sure that there is a MiniMyth configuration file
    if [ ! -e /etc/minimyth.d/minimyth.conf.remote ] ; then
        if mm_url_mount partlbl://BOOTFS /mnt/boot ; then
            /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/conf_sequential] mounting /BOOTFS part into /nmt/boot ..."
            if [ -e /mnt/boot/minimyth.conf ] ; then
                /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/conf_sequential] local minimyth.conf at /boot found. Will use it ..."
                /bin/cp -f /mnt/boot/minimyth.conf /etc/minimyth.d/minimyth.conf
                mm_message_output info "MiniMyth2 will use configuration from /boot partition ..."
            fi
        else
            /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/conf_sequential] No minimyth.conf found at remore nor /boot. Will use buildd-in defaults..."
            mm_message_output info "MiniMyth2 will use build-in default configuration ..."
        fi
    else
        /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/conf_sequential] minimyth.conf downloaded sucessfully. Will use it..."
        /bin/mv -f /etc/minimyth.d/minimyth.conf.remote /etc/minimyth.d/minimyth.conf
        mm_message_output info "MiniMyth2 will use configuration from remote server ..."
    fi

    # Read MiniMyth configuration file
    . /etc/minimyth.d/minimyth.conf

    # Process the DHCP override configuration variables
    # so that they are available to the DHCP client
    /bin/rm -f '/etc/conf.d/dhcp.override~'
    /bin/touch '/etc/conf.d/dhcp.override~'
    /bin/chmod 644 '/etc/conf.d/dhcp.override~'
    . /etc/rc.d/init.d/conf.d/MM_DHCP
    conf_write_MM_DHCP '/etc/conf.d/dhcp.override~'
    /bin/rm -f '/etc/conf.d/dhcp.override~~'
    /bin/touch '/etc/conf.d/dhcp.override~~'
    /bin/chmod 644 '/etc/conf.d/dhcp.override~~'
    /usr/bin/sort '/etc/conf.d/dhcp.override~' > '/etc/conf.d/dhcp.override~~'
    /bin/mv -f '/etc/conf.d/dhcp.override~~' '/etc/conf.d/dhcp.override~'
    /bin/mv -f '/etc/conf.d/dhcp.override~' '/etc/conf.d/dhcp.override'

    # Fetch and run 'minimyth.script'
    if [ "x${MM_MINIMYTH_FETCH_MINIMYTH_SCRIPT}" = "xyes" ] ; then
        /bin/rm -f /etc/minimyth.d/minimyth.script
        mm_confro_get /minimyth.script /etc/minimyth.d/minimyth.script
        if [ ! -e /etc/minimyth.d/minimyth.script ] ; then
            mm_message_output err "error: failed to fetch 'minimyth.script' file."
        else
            /bin/chmod 0755 /etc/minimyth.d/minimyth.script
            /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/conf_sequential] minimyth.script downloaded successfuly. Will execute it..."
        fi
    fi
    if [ -f /etc/minimyth.d/minimyth.script ] ; then
        mm_message_output info "executing user minimyth.script ..."
        # Make unprocessed MiniMyth variables visable to 'minimyth.script'
        /bin/rm -f '/etc/conf.d/minimyth.raw'
        /bin/touch '/etc/conf.d/minimyth.raw'
        /bin/chmod 644 '/etc/conf.d/minimyth.raw'
        set | grep '^MM_' >> '/etc/conf.d/minimyth.raw'
        # Run 'minimyth.script'
        /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/conf_sequential] Executing minimyth.script..."
        /bin/sh /etc/minimyth.d/minimyth.script
        # Delete file with unprocessed MiniMyth variables
        /bin/rm -f '/etc/conf.d/minimyth.raw'
    fi

    # Enable configuration auto-detection udev rules
    /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/conf_sequential] enabling minimyth udev rules..."
    file_list=`/bin/ls -1 /usr/lib/udev/rules.d/05-minimyth-*.rules.disabled | /bin/sed -e 's%\.disabled%%'`
    for file in ${file_list} ; do
        /bin/mv -f ${file}.disabled ${file}
    done

    # Trigger udev with the additional udev rules that handle configuration auto-detection
    /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/conf_sequential] retrigering udev with enabled minimyth udev rules..."
    /usr/bin/udevadm control --reload
    /usr/bin/udevadm trigger --action=add
    /usr/bin/udevadm settle --timeout=60

    file_list=`cd /etc/rc.d/init.d/conf.d ; /bin/ls -1 S_*`

    # Read MiniMyth configuration processing files
    for file in ${file_list} ; do
        . /etc/rc.d/init.d/conf.d/${file}
    done

    # Restart the DHCP client in order to pick up the processed DHCP override variables
    /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/conf_sequential] restarting DHCP client with config values..."
    /etc/rc.d/init.d/dhcp start

    # Write processed MiniMyth configuration file
    mm_message_output info "downloading files and writing configuration ..."
    /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/conf_sequential] processing configuration: writing variables..."
    /bin/rm -f '/etc/conf.d/minimyth~'
    /bin/touch '/etc/conf.d/minimyth~'
    /bin/chmod 644 '/etc/conf.d/minimyth~'
    for file in ${file_list} ; do
        conf_write_${file} '/etc/conf.d/minimyth~'
        # /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/conf_sequential] conf_write_${file} done..."
    done
    /bin/rm -f '/etc/conf.d/minimyth~~'
    /bin/touch '/etc/conf.d/minimyth~~'
    /bin/chmod 644 '/etc/conf.d/minimyth~~'
    /usr/bin/sort '/etc/conf.d/minimyth~' > '/etc/conf.d/minimyth~~'
    /bin/mv -f '/etc/conf.d/minimyth~~' '/etc/conf.d/minimyth~' 
    /bin/mv -f '/etc/conf.d/minimyth~' '/etc/conf.d/minimyth' 

    # Writing variables
    /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/conf_sequential] updating manage scripts with BE IP='${MM_MASTER_SERVER}'"
    /bin/sed -e "s/@MM_MASTER_SERVER@/${MM_MASTER_SERVER}/g" -i /usr/bin/mm_show_systat_osd.pl
    /bin/sed -e "s/@MM_MASTER_SERVER@/${MM_MASTER_SERVER}/g" -i /usr/bin/mm_manage
    /bin/sed -e "s/@MM_MASTER_SERVER@/${MM_MASTER_SERVER}/g" -i /usr/bin/mm_notify_updates
    /bin/sed -e "s/@MM_MASTER_SERVER@/${MM_MASTER_SERVER}/g" -i /usr/bin/mm_start_button_actions

    if [ -z "${MM_MYTHTV_HOME_URL}" ] ; then
        if [ ! "x${MM_MYTHTV_HOME_RAMDISK}" = "xno" ] ; then
            /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/media] Creating ${MM_MYTHTV_HOME_RAMDISK}MB RAMdisk for minimyth home dir..."
            /bin/mkdir -p /var/minimyth.home
            /bin/cp -af /home/minimyth/. /var/minimyth.home/
            /bin/rm -rf /home/minimyth/.
            /bin/mount -t tmpfs -o nodev,nosuid,size=${MM_MYTHTV_HOME_RAMDISK}M,mode=0755 tmpfs /home/minimyth
            /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/media] Start using RAMdisk as minimyth home dir..."
            /bin/cp -af /var/minimyth.home/. /home/minimyth/
            /bin/chown -R minimyth:minimyth /home/minimyth
            /bin/rm -rf /var/minimyth.home/
        fi
    fi

    # If there are any errors, then do not continue
    if [ -e /var/log/minimyth.err.log ] && [ `/bin/cat /var/log/minimyth.err.log | /usr/bin/wc -l` -gt 0 ] ; then
        mm_message_output err "error: check '/var/log/minimyth.err.log' for details."
        exit 1
    fi

    /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/conf_sequential] Started with return code=0"

    return 0
}

stop() {
    return 0
}

case $1 in
    start) start ;;
    stop)  stop  ;;
esac

exit 0
